import java.util.InputMismatchException;
import java.util.Scanner;

public class Main {
	// considered the GUI that will be created later on, there is no while loop in this program to keep asking valid input from user if the input is invalid
	public static void main(String args[]){
		System.out.println("IMPORTANT: This program should not be use for real dive.");
			
		int[][] diveTime = new int[][]{
			{5, 15, 25, 30, 40, 50, 70, 80, 100, 110, 130, 150},
			{0, 10, 15, 25, 30, 40, 50, 60, 70, 80, 0, 100},
			{0, 10, 15, 20, 25, 30, 40, 50, 55, 60, 0, 80},
			{0, 5, 10, 15, 20, 30, 35, 40, 45, 50, 60, 70},
			{0, 5, 10, 15, 20, 25, 30, 35, 40, 0, 50, 60},
			{0, 5, 10, 12, 15, 20, 25, 30, 0, 40, 0, 50},
			{0, 5, 7, 10, 15, 20, 22, 25, 0, 0, 40, 0},
			{0, 0, 5, 10, 13, 15, 20, 0, 0, 30, 0, 0},
			{0, 0, 5, 10, 12, 15, 0, 0, 25, 30, 0, 0},
			{0, 0, 5, 8, 10, 0, 0, 0, 0, 25, 0, 0}
		};
				
		double[][] surfaceTime = new double[][]{
			{24.00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
			{24.00, 3.20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
			{24.00, 4.49, 1.39, 0, 0, 0, 0, 0, 0, 0, 0, 0},
			{24.00, 5.48, 2.38, 1.09, 0, 0, 0, 0, 0, 0, 0, 0},
			{24.00, 6.34, 3.24, 1.57, 0.54, 0, 0, 0, 0, 0, 0, 0},
			{24.00, 7.05, 3.57, 2.28, 1.29, 0.45, 0, 0, 0, 0, 0, 0},
			{24.00, 7.35, 4.25, 2.58, 1.59, 1.15, 0.40, 0, 0, 0, 0, 0},
			{24.00, 7.59, 4.49, 3.20, 2.23, 1.41, 1.06, 0.36, 0, 0, 0, 0},
			{24.00, 8.21, 5.12, 3.43, 2.44, 2.02, 1.29, 0.59, 0.33, 0, 0, 0},
			{24.00, 8.50, 5.40, 4.02, 3.04, 2.20, 1.47, 1.19, 0.54, 0.31, 0, 0},
			{24.00, 8.58, 5.48, 4.19, 3.21, 2.38, 2.03, 1.35, 1.11, 0.49, 0.28, 0},
			{24.00, 9.12, 6.02, 4.35, 3.36, 2.53, 2.19, 1.49, 1.25, 1.04, 0.45, 0.26},
		};
		
		int[][] nitrogenTime = new int[][]{
			{7, 6, 5, 4, 4, 3, 3, 3, 3, 3},
			{17, 13, 11, 9, 8, 7, 7, 6, 6, 6},
			{25, 21, 17, 15, 13, 11, 10, 10, 9, 8},
			{37, 29, 24, 20, 18, 16, 14, 13, 12, 11},
			{49, 38, 30, 26, 23, 20, 18, 16, 15, 13},
			{61, 47, 36, 31, 28, 24, 22, 20, 18, 16},
			{73, 56, 44, 37, 32, 29, 26, 24, 21, 19},
			{87, 66, 52, 43, 38, 33, 30, 27, 25, 22},
			{101, 76, 61, 50, 43, 38, 34, 31, 28, 25},
			{116, 87, 70, 57, 48, 43, 38, 0, 0, 0},
			{138, 99, 79, 64, 54, 47, 0, 0, 0, 0},
			{161, 111, 88, 72, 61, 53, 0, 0, 0, 0},
		};
				
		Scanner scan = new Scanner(System.in);
		
		double depthInput;
		double timeInput;
		double surfaceInput;
		int depthIndex = 0;
		int timeIndex = 0;
		int surfaceIndex = 0;
		char pressureGroup = ' ';
		boolean valid = false;
		double totalNitrogen;
		int anotherDive;

		try{
			System.out.print("Enter the depth for the first dive (in feet): ");
			depthInput = scan.nextDouble();
			
			// displays warning messages if depth input is invalid or find the index of the array the depthInput is in if the depth input is valid
			depthIndex = findDepthIndex(depthInput);
			
			if(depthInput > 0 && depthInput < 130){				
				System.out.print("Enter the dive time for the first dive (in minutes): ");
				timeInput = scan.nextDouble();

				// displays warning messages if dive time input is less than 0 minutes or find the index of the array the timeInput is in if the dive time input is valid
				timeIndex = findTimeIndex(diveTime, depthIndex, timeInput);
				
				if(timeInput > 0){	
					// displays warning messages if dive time input is greater than the maximum dive time and displays the time for a safety stop
					valid = safetyStop(depthIndex, timeInput);
					
					// ask the user if he/she wants to have another dive
					if(valid == true){
						valid = false;
						
						// find the pressure group
						pressureGroup = pressureGroup(timeIndex);
						System.out.println("You are currently in the pressure group of: " + pressureGroup);
					
						System.out.print("\nDo you want to have another dive (1: Yes, 2: No)? ");						
						anotherDive = scan.nextInt();
						
						// if yes, find the new pressure group base on the surface interval time
						while(anotherDive == 1){	
							anotherDive = 0;
									
							System.out.print("Enter the surface interval time (in hours.minutes): ");
							surfaceInput = scan.nextDouble();
								
							// displays warning messages if surface interval time input is invalid or find the index of the array the surfaceInput is in if the surface interval time input is valid
							surfaceIndex = findSurfaceIndex(surfaceTime, timeIndex, surfaceInput);
							
							if(surfaceInput >= 0.10 && surfaceInput < 24){
								//find the new pressure group
								pressureGroup = pressureGroup(surfaceIndex);
								System.out.println("Your new pressure group is: " + pressureGroup);
								
								System.out.print("\nEnter the depth for the new dive (in feet): ");
								depthInput = scan.nextDouble();
								
								// displays warning messages if depth input is invalid or find the index of the array the depthInput is in if the depth input is valid
								depthIndex = findDepthIndex(depthInput);
								
								if(nitrogenTime[surfaceIndex][depthIndex] == 0){
									System.out.println("WARNING: Diving in this depth while in this pressure group is not allowed.");
								}
								else if(depthInput > 0 && depthInput < 130){				
									System.out.print("Enter the dive time for the new dive (in minutes): ");
									timeInput = scan.nextDouble();
									
									// calculates the total nitrogen time
									totalNitrogen = timeInput + nitrogenTime[surfaceIndex][depthIndex];
									System.out.println("Your total nitrogen time is " + totalNitrogen + " minutes.");
									
									// displays warning messages if total nitrogen time is less than 0 minutes or find the index of the array the totalNitrogen is in if the total nitrogen time is valid
									timeIndex = findTimeIndex(diveTime, depthIndex, totalNitrogen);
									
									if(timeInput > 0){	
										// displays warning messages if dive time input is greater than the maximum dive time and displays the time for a safety stop
										valid = safetyStop(depthIndex, totalNitrogen);
										
										// ask the user if he/she wants to have another dive
										if(valid == true){
											valid = false;
											
											// find the new pressure group
											pressureGroup = pressureGroup(timeIndex);
											System.out.println("Your new pressure group is: " + pressureGroup);
										
											System.out.print("\nDo you want to have another dive (1: Yes, 2: No)? ");						
											anotherDive = scan.nextInt();
										}
									}
								}	
							}
						}
					}				
				}				
			}
		}
		catch(InputMismatchException e){
			System.out.println("WARNING: Please enter a valid input.");
			String temp = scan.nextLine();
		}
	}	

	
	/** Displays warning messages if depth input is invalid or find the index of the array the depthInput is in if the depth input is valid */
	public static int findDepthIndex(double depthInput){				
		// creates an array for depth in feet
		int[] depth = {40, 50, 60, 70, 80, 90, 100, 110, 120, 130};
				
		// the array length of depth
		int depthArray = 10;
		int depthIndex = 0;
		
		if(depthInput <= 0){
			System.out.println("WARNING: The minimum depth must be greater than 0 ft.");
		}
		else if(depthInput > 130){
			System.out.println("WARNING: The maxiumum depth is 130 ft.");
		}
		else if(depthInput > 0 && depthInput <= 15){
			System.out.println("You are in the safe depth. A safety stop and a break between dives is not required.");
		}
		else{
			// find the index of the array the depthInput is in 
			for(int i = 0; i < depthArray; i++){
				if(depthInput <= depth[i]){
					depthIndex = i;
					// stop the loop as it finds the index
					depthArray = i;
				}
			}
		}
		return depthIndex;
	}
	
	
	/** Displays warning messages if dive time input is less than 0 minutes or find the index of the array the timeInput is in if the dive time input is valid */
	public static int findTimeIndex(int[][] diveTime, int depthIndex, double timeInput){
		// the array length of diveTime
		int timeArray = 12;
		int timeIndex = 0;
		
		if(timeInput <= 0){
			System.out.println("WARNING: The minimum dive time must be greater than 0 minutes.");
		}
		else{
			// find the index of the array the timeInput is in
			for(int j = 0; j < timeArray; j++){
				if(timeInput <= diveTime[depthIndex][j]){
					timeIndex = j;
					// stop the loop as it finds the index
					timeArray = j;
				}
			}			
		}
		return timeIndex;
	}
	
	
	/* Find the pressure group. */
	public static char pressureGroup(int timeIndex){
		char pressureGroup = ' ';
		
		if(timeIndex == 0){
			pressureGroup = 'A';
		}
		else if(timeIndex == 1){
			pressureGroup = 'B';
		}
		else if(timeIndex == 2){
			pressureGroup = 'C';
		}
		else if(timeIndex == 3){
			pressureGroup = 'D';
		}
		else if(timeIndex == 4){
			pressureGroup = 'E';
		}
		else if(timeIndex == 5){
			pressureGroup = 'F';
		}
		else if(timeIndex == 6){
			pressureGroup = 'G';
		}
		else if(timeIndex == 7){
			pressureGroup = 'H';
		}
		else if(timeIndex == 8){
			pressureGroup = 'I';
		}
		else if(timeIndex == 9){
			pressureGroup = 'J';
		}
		else if(timeIndex == 10){
			pressureGroup = 'K';
		}
		else if(timeIndex == 11){
			pressureGroup = 'L';
		}
		return pressureGroup;
	}
	
	
	/**
	* Displays warning messages if dive time input is greater than the maximum dive time and displays the time for a safety stop.
	* @param depthIndex The index of the array the depthInput is in.
	* @param timeInput
	* @return valid to check if the dive time input is valid. 
	*/
	public static boolean safetyStop(int depthIndex, double timeInput){
		boolean valid = false;
		
		// if depthIndex > 15 && depthInput <= 40
		if(depthIndex == 0 && timeInput <= 130){
			System.out.println("A 3 minutes safety stop at 15 ft is recommended.");
			valid = true;
		}
		else if(depthIndex == 0 && timeInput > 130 && timeInput <= 150){
			System.out.println("WARNING: The maximum dive time in this depth is 130 minutes.");
			System.out.println("WARNING: A safety stop at 15 ft for 5 minutes is required.");
			valid = true;
		}
		else if(depthIndex == 0 && timeInput > 150){
			System.out.println("WARNING: Diving in this depth in this amount of time is not allowed.");
		}
		
		// if depthIndex > 40 && depthInput <= 50
		if(depthIndex == 1 && timeInput <= 80){
			System.out.println("A 3 minutes safety stop at 15 ft is recommended.");
			valid = true;
		}
		else if(depthIndex == 1 && timeInput > 80 && timeInput <= 100){
			System.out.println("WARNING: The maximum dive time in this depth is 80 minutes.");
			System.out.println("WARNING: A safety stop at 15 ft for 5 minutes is required.");
			valid = true;
		}
		else if(depthIndex == 1 && timeInput > 100){
			System.out.println("WARNING: Diving in this depth in this amount of time is not allowed.");
		}
		
		// if depthIndex > 50 && depthInput <= 60
		if(depthIndex == 2 && timeInput <= 55){
			System.out.println("A 3 minutes safety stop at 15 ft is recommended.");
			valid = true;
		}
		else if(depthIndex == 2 && timeInput > 55 && timeInput <= 60){
			System.out.println("WARNING: The maximum dive time in this depth is 55 minutes.");
			System.out.println("WARNING: A safety stop at 15 ft for 5 minutes is required.");
			valid = true;
		}
		else if(depthIndex == 2 && timeInput > 60 && timeInput <= 80){
			System.out.println("WARNING: The maximum dive time in this depth is 55 minutes.");
			System.out.println("WARNING: A safety stop at 15 ft for 7 minutes is required.");
			valid = true;
		}
		else if(depthIndex == 2 && timeInput > 80){
			System.out.println("WARNING: Diving in this depth in this amount of time is not allowed.");
		}
		
		// if depthIndex > 60 && depthInput <= 70
		if(depthIndex == 3 && timeInput <= 45){
			System.out.println("A 3 minutes safety stop at 15 ft is recommended.");
			valid = true;
		}
		else if(depthIndex == 3 && timeInput > 45 && timeInput <= 50){
			System.out.println("WARNING: The maximum dive time in this depth is 45 minutes.");
			System.out.println("WARNING: A safety stop at 15 ft for 5 minutes is required.");
			valid = true;
		}
		else if(depthIndex == 3 && timeInput > 50 && timeInput <= 60){
			System.out.println("WARNING: The maximum dive time in this depth is 45 minutes.");
			System.out.println("WARNING: A safety stop at 15 ft for 8 minutes is required.");
			valid = true;
		}
		else if(depthIndex == 3 && timeInput > 60 && timeInput <= 70){
			System.out.println("WARNING: The maximum dive time in this depth is 45 minutes.");
			System.out.println("WARNING: A safety stop at 15 ft for 14 minutes is required.");
			valid = true;
		}
		else if(depthIndex == 3 && timeInput > 70){
			System.out.println("WARNING: Diving in this depth in this amount of time is not allowed.");
		}
		
		// if depthIndex > 70 && depthInput <= 80
		if(depthIndex == 4 && timeInput <= 35){
			System.out.println("A 3 minutes safety stop at 15 ft is recommended.");
			valid = true;
		}
		else if(depthIndex == 4 && timeInput > 35 && timeInput <= 40){
			System.out.println("WARNING: The maximum dive time in this depth is 35 minutes.");
			System.out.println("WARNING: A safety stop at 15 ft for 5 minutes is required.");
			valid = true;
		}
		else if(depthIndex == 4 && timeInput > 40 && timeInput <= 50){
			System.out.println("WARNING: The maximum dive time in this depth is 35 minutes.");
			System.out.println("WARNING: A safety stop at 15 ft for 10 minutes is required.");
			valid = true;
		}
		else if(depthIndex == 4 && timeInput > 50 && timeInput <= 60){
			System.out.println("WARNING: The maximum dive time in this depth is 35 minutes.");
			System.out.println("WARNING: A safety stop at 15 ft for 17 minutes is required.");
			valid = true;
		}
		else if(depthIndex == 4 && timeInput > 60){
			System.out.println("WARNING: Diving in this depth in this amount of time is not allowed.");
		}
		
		// if depthIndex > 80 && depthInput <= 90
		if(depthIndex == 5 && timeInput <= 25){
			System.out.println("A 3 minutes safety stop at 15 ft is recommended.");
			valid = true;
		}
		else if(depthIndex == 5 && timeInput > 25 && timeInput <= 30){
			System.out.println("WARNING: The maximum dive time in this depth is 25 minutes.");
			System.out.println("WARNING: A safety stop at 15 ft for 5 minutes is required.");
			valid = true;
		}
		else if(depthIndex == 5 && timeInput > 30 && timeInput <= 40){
			System.out.println("WARNING: The maximum dive time in this depth is 25 minutes.");
			System.out.println("WARNING: A safety stop at 15 ft for 7 minutes is required.");
			valid = true;
		}
		else if(depthIndex == 5 && timeInput > 40 && timeInput <= 50){
			System.out.println("WARNING: The maximum dive time in this depth is 25 minutes.");
			System.out.println("WARNING: A safety stop at 15 ft for 18 minutes is required.");
			valid = true;
		}
		else if(depthIndex == 5 && timeInput > 50){
			System.out.println("WARNING: Diving in this depth in this amount of time is not allowed.");
		}
		
		// if depthIndex > 90 && depthInput <= 100
		if(depthIndex == 6 && timeInput <= 22){
			System.out.println("A 3 minutes safety stop at 15 ft is recommended.");
			valid = true;
		}
		else if(depthIndex == 6 && timeInput > 22 && timeInput <= 25){
			System.out.println("WARNING: The maximum dive time in this depth is 22 minutes.");
			System.out.println("WARNING: A safety stop at 15 ft for 5 minutes is required.");
			valid = true;
		}
		else if(depthIndex == 6 && timeInput > 25 && timeInput <= 40){
			System.out.println("WARNING: The maximum dive time in this depth is 22 minutes.");
			System.out.println("WARNING: A safety stop at 15 ft for 15 minutes is required.");
			valid = true;
		}
		else if(depthIndex == 6 && timeInput > 40){
			System.out.println("WARNING: Diving in this depth in this amount of time is not allowed.");
		}
		
		// if depthIndex > 100 && depthInput <= 110
		if(depthIndex == 7 && timeInput <= 15){
			System.out.println("A 3 minutes safety stop at 15 ft is recommended.");
			valid = true;
		}
		else if(depthIndex == 7 && timeInput > 15 && timeInput <= 20){
			System.out.println("WARNING: The maximum dive time in this depth is 15 minutes.");
			System.out.println("WARNING: A safety stop at 15 ft for 5 minutes is required.");
			valid = true;
		}
		else if(depthIndex == 7 && timeInput > 20 && timeInput <= 30){
			System.out.println("WARNING: The maximum dive time in this depth is 15 minutes.");
			System.out.println("WARNING: A safety stop at 15 ft for 7 minutes is required.");
			valid = true;
		}
		else if(depthIndex == 7 && timeInput > 30){
			System.out.println("WARNING: Diving in this depth in this amount of time is not allowed.");
		}
		
		// if depthIndex > 110 && depthInput <= 120
		if(depthIndex == 8 && timeInput <= 12){
			System.out.println("A 3 minutes safety stop at 15 ft is recommended.");
			valid = true;
		}
		else if(depthIndex == 8 && timeInput > 12 && timeInput <= 15){
			System.out.println("WARNING: The maximum dive time in this depth is 12 minutes.");
			System.out.println("WARNING: A safety stop at 15 ft for 5 minutes is required.");
			valid = true;
		}
		else if(depthIndex == 8 && timeInput > 15 && timeInput <= 25){
			System.out.println("WARNING: The maximum dive time in this depth is 12 minutes.");
			System.out.println("WARNING: A safety stop at 15 ft for 6 minutes is required.");
			valid = true;
		}
		else if(depthIndex == 8 && timeInput > 25 && timeInput <= 30){
			System.out.println("WARNING: The maximum dive time in this depth is 12 minutes.");
			System.out.println("WARNING: A safety stop at 15 ft for 14 minutes is required.");
			valid = true;
		}
		else if(depthIndex == 8 && timeInput > 30){
			System.out.println("WARNING: Diving in this depth in this amount of time is not allowed.");
		}
		
		// if depthIndex > 120 && depthInput <= 130
		if(depthIndex == 9 && timeInput <= 8){
			System.out.println("A 3 minutes safety stop at 15 ft is recommended.");
			valid = true;
		}
		else if(depthIndex == 9 && timeInput > 8 && timeInput <= 10){
			System.out.println("WARNING: The maximum dive time in this depth is 8 minutes.");
			System.out.println("WARNING: A safety stop at 15 ft for 5 minutes is required.");
			valid = true;
		}
		else if(depthIndex == 9 && timeInput > 10 && timeInput <= 25){
			System.out.println("WARNING: The maximum dive time in this depth is 12 minutes.");
			System.out.println("WARNING: A safety stop at 15 ft for 10 minutes is required.");
			valid = true;
		}
		else if(depthIndex == 9 && timeInput > 25){
			System.out.println("WARNING: Diving in this depth in this amount of time is not allowed.");
		}
		return valid;
	}
	
	
	/** Displays warning messages if surface interval time input is invalid or find the index of the array the surfaceInput is in if the surface interval time input is valid */
	public static int findSurfaceIndex(double[][]surfaceTime, int timeIndex, double surfaceInput){
		// the array length of surfaceTime
		int surfaceArray = 12;
		int surfaceIndex = 0;
		
		if(surfaceInput < 0.10){
			System.out.println("WARNING: The minimum surface interval time must be greater than or equal to 10 minutes.");
		}
		else if(surfaceInput > 24){
			System.out.println("You are not placing into a new pressure group since you are safe to have another dive after a 24 hours of break.");
		}
		else{
			// find the index of the array the surfaceInput is in
			for(int k = 0; k < surfaceArray; k++){
				if(surfaceInput > surfaceTime[timeIndex][k]){
					surfaceIndex = k - 1;
					// stop the loop as it finds the index
					surfaceArray = k;
				}
			}
		}
		return surfaceIndex;
	}

}